﻿using SeriesManager.Model;
using System.Collections.Generic;

namespace SeriesManager.Model.Comparer
{
    public class EpisodeComparer : IComparer<EpisodeModel>
    {
        bool airDate;

        public EpisodeComparer(bool airDate = false)
        {
            this.airDate = airDate;
        }

        public int Compare(EpisodeModel x, EpisodeModel y)
        {
            if (this.airDate)
            {
                return CompareByAirDate(x, y);
            }
            else
            {
                return CompareBySeasonNumber(x, y);
            }
        }

        private int CompareBySeasonNumber(EpisodeModel x, EpisodeModel y)
        {
            var season = x.SeasonNumber.CompareTo(y.SeasonNumber);

            if (season < 0)
            {
                return -1;
            }
            else if (season == 0)
            {
                return x.Number.CompareTo(y.Number);
            }
            else
            {
                return +1;
            }
        }

        private int CompareByAirDate(EpisodeModel x, EpisodeModel y)
        {
            return x.FirstAired.CompareTo(y.FirstAired);
        }
    }
}
